#define __ANT_PROC_C__	1   /* This line must be on top of the file! */

#include "config.h"

void antProcNaviBoardWithAPPIDSend(U8 appID,U8 cmdID,U8 *p,U8 len)
{
	U8 crc,i;
	if (FIFO_GET_IDEL_COUNT(Uart2TxFifo) >= 2+1+2+len+1)
	{
		FIFO_PUT_ONE(Uart2TxFifo,0x55);
		FIFO_PUT_ONE(Uart2TxFifo,0xAA);
		FIFO_PUT_ONE(Uart2TxFifo,2+len);
		crc = 2+len;
		FIFO_PUT_ONE(Uart2TxFifo,appID);
		crc += appID;
		FIFO_PUT_ONE(Uart2TxFifo,cmdID);
		crc += cmdID;
		if (p && len)
		{
			for (i = 0;i < len;i++)
			{
				crc += p[i];
			}
			FIFO_PUT_STR(Uart2TxFifo,p,len);
		}
	}
	FIFO_PUT_ONE(Uart2TxFifo,crc);
    if (appID==APP_SYSTEM && 0x40 == cmdID)
    {
        return;
    }
    
	Trace("SendNaviMsg %02X %02X ",appID,cmdID);
    for (i = 0;i < len;i++)
    {
        Trace("%02X ",p[i]);
    }
    Trace("\r\n");
}

void AntProcProc(U8 rx)
{
//	printf("UARTNavi:0x%02X\r\n",rx);
	switch (antProcInfo.rxStatus)
	{
		case 0:
			if (0x55 == rx)
			{
				antProcInfo.rxStatus = 1;
			}
			break;
		case 1:
			if (0xAA == rx)
			{
				antProcInfo.rxStatus = 2;
			}
			else if (0x55 == rx)
			{
			}
			else
			{
				antProcInfo.rxStatus = 0;
			}
			break;
		case 2:
			antProcInfo.rxLength = rx;
			antProcInfo.rxLengthHave = 0;
			antProcInfo.rxCRC = rx;
			antProcInfo.rxStatus = 3;
			if (antProcInfo.rxLength > ANT_RX_BUFF_COUNT)
			{
				printf("$$OVER: AntProcProc Message Too Long!\r\n");
				antProcInfo.rxStatus = 0;
			}
			break;
		case 3:
			if (antProcInfo.rxLengthHave < antProcInfo.rxLength)
			{
				antProcInfo.rxBuff[antProcInfo.rxLengthHave++] = rx;
				antProcInfo.rxCRC += rx;
			}
			else
			{
				if (antProcInfo.rxCRC == rx)
				{
					dealNaviBoardData(antProcInfo.rxBuff,antProcInfo.rxLength);
				}
				else
				{
                    u8 i;
                    Trace("Uart2 Rx count:%d, rx:%02X, crc:%02x\r\n",FIFO_GET_COUNT(Uart2RxFifo),rx,antProcInfo.rxCRC);
					printf("$$OVER: AntProcProc CRC Error!\r\n");
                    for(i=0;i<antProcInfo.rxLengthHave;i++)
                    {
                        Trace("%02X ",antProcInfo.rxBuff[i]);
                    }
                    Trace("\r\n");
				}
				antProcInfo.rxStatus = 0;
			}
			break;
		default:
			antProcInfo.rxStatus = 0;
			break;
	}
}

void AntProc_Init(void)
{
	memset(&antProcInfo,0,sizeof(ANT_PROC_T));
}

/*________________________________END OF FILE_________________________________*/


